package com.evil.common.activiti.enums;

import com.evil.common.core.enums.BaseEnum;
import com.evil.common.family.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 工作流处理类型
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum HandleTypeEnum {

    /**
     * 工作流处理类型
     */
    START(1, "发起新的", HandleTypeClassifyEnum.START),
    PROCESSING(2, "处理中", HandleTypeClassifyEnum.PROCESSING),
    SUBMIT(3, "提交处理", HandleTypeClassifyEnum.HANDLED),
    CARBON(4, "抄送", HandleTypeClassifyEnum.CARBON),
    FALLBACK(5, "回退", HandleTypeClassifyEnum.HANDLED),
    APPROVED(6, "审批通过", HandleTypeClassifyEnum.HANDLED),
    REJECTED(7, "审批拒绝", HandleTypeClassifyEnum.HANDLED),
    REASSIGNMENT(8, "待办转交", HandleTypeClassifyEnum.PROCESSING),
    SUSPEND(9, "挂起", HandleTypeClassifyEnum.SUSPEND),
    ADD_ROBOT(10, "添加数据", HandleTypeClassifyEnum.ROBOT),
    MODIFY_ROBOT(11, "更新数据", HandleTypeClassifyEnum.ROBOT),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;
    /**
     * 处理类型分
     */
    @Getter
    private final HandleTypeClassifyEnum classify;

    public static Optional<HandleTypeEnum> idExistById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static HandleTypeEnum findById(Integer id) {
        return HandleTypeEnum.idExistById(id).orElseThrow(HandleTypeEnum::getBusinessException);
    }

    /**
     * 获取 已处理
     *
     * @return List
     */
    public static List<Integer> getHandledType() {
        return HandleTypeEnum.findByClassify(HandleTypeClassifyEnum.HANDLED);
    }

    public static boolean isHandled(Integer id) {
        return HandleTypeEnum.getHandledType().contains(id);
    }

    public static List<Integer> findByClassify(HandleTypeClassifyEnum classifyEnum) {
        if (null == classifyEnum) {
            return new ArrayList<>();
        }
        return Arrays.stream(values())
                .filter(e -> e.classify.getId() == classifyEnum.getId())
                .map(HandleTypeEnum::getId)
                .collect(Collectors.toList());
    }

    /**
     * 提交/通过
     *
     * @param id id
     * @return boolean
     */
    public static boolean isPass(Integer id) {
        // 发起新的、提交处理、审批通过
        return Arrays.asList(START.id, SUBMIT.id, APPROVED.id).contains(id);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(BaseEnum.ERROR_PARAMETER, "工作流处理类型参数错误");
    }
}
